//
//  UserSignatureCell.m
//  CheekiOS
//
//  Created by mac on 2017/6/15.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserSignatureCell.h"

@implementation UserSignatureCell

+(instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *UserSignatureCellident = @"UserSignatureCellident";
    UserSignatureCell *cell  = [tableView dequeueReusableCellWithIdentifier:UserSignatureCellident];
    if (!cell) {
        cell = [[UserSignatureCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserSignatureCellident];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        headeImage = [[UIImageView alloc] init];
        headeImage.contentMode = UIViewContentModeScaleAspectFill;
        
        [headeImage.layer setMasksToBounds:YES];
        CALayer * layer = [headeImage layer];
        layer.borderColor = [[UIColor whiteColor] CGColor];
        layer.borderWidth = 2.0f;
        [self addSubview:headeImage];
        
        CGFloat signatureFont ;
        if (iPhone5) {
            signatureFont = 13;
        }else if(iPhone6){
            signatureFont = 14;
        }else{
            signatureFont = 15;
        }
        
        SignaLab = [[UILabel alloc] init];
        SignaLab.font = FontP(signatureFont);
        SignaLab.textColor = ColorI(0x2e353f);
        [self addSubview:SignaLab];
        
        SignatureLab = [[UILabel alloc] init];
        SignatureLab.font = FontP(signatureFont);
        SignatureLab.textColor = RGB(190, 190, 193);
        SignatureLab.textAlignment=NSTextAlignmentLeft;
        
        [self addSubview:SignatureLab];
        
        
        
        _IdNumberLab = [UILabel new];
        
        _IdNumberLab.font = FontP(12);
        _IdNumberLab.textColor = ColorI(0xffffff);
        _IdNumberLab.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_IdNumberLab];
        
        _nameLab = [UILabel new];
        
        _nameLab.font = FontBold(20);
        _nameLab.textColor = ColorI(0xffffff);
        _nameLab.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_nameLab];
        
        ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        ageBtn.enabled = NO;
        ageBtn.layer.cornerRadius = 4;
        ageBtn.layer.masksToBounds = YES;
        [ageBtn setTitleColor:Theme_Color_White forState:UIControlStateNormal];
        ageBtn .imageEdgeInsets = UIEdgeInsetsMake(0,-5,0,0);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
        ageBtn.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
        
        
        [self addSubview:ageBtn];
        
        _constellationLab = [UILabel new];
        _constellationLab.textColor = Theme_Color_White;
        _constellationLab.layer.cornerRadius = 4;
        _constellationLab.layer.masksToBounds = YES;
        _constellationLab.backgroundColor = RandomColor;
        
        _constellationLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_constellationLab];
        
        
        if (iPhone5) {
            _constellationLab.font = FontP(12);
            ageBtn.titleLabel.font = FontBold(12);
            
        }else{
            _constellationLab.font = FontP(14);
            ageBtn.titleLabel.font = FontBold(14);
        }
    }
    return self;
}

-(void)setInfoModel:(UserpersionlInfoModel *)infoModel{

    SignaLab.text = @"个性签名";
    
    CGSize signaSize = textSizeFont(SignaLab.text, SignaLab.font, KScreenWidth, 20);
    SignaLab.frame = CGRectMake(15, 12.5, signaSize.width, signaSize.height);
    
    
    _nameLab.text = infoModel.nickName;
    _IdNumberLab.text = [NSString stringWithFormat:@"ID%    ld", infoModel.uid];
    if (infoModel.sex == 0) {
        [ageBtn setBackgroundColor:ColorI(0xf28484)];
        
        [ageBtn setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    }else{
        [ageBtn setBackgroundColor:ColorI(0x84b9f2)];
        
        [ageBtn setImage:[UIImage imageNamed:@"male"] forState:UIControlStateNormal];
    }
    
    [ageBtn setTitle:infoModel.age  forState:UIControlStateNormal];
    _constellationLab.backgroundColor = [self getColorWithStr:infoModel.star];
    
    _constellationLab.text = infoModel.star;
    
    
    
    CGSize IdNumberLabSize = textSizeFont(_IdNumberLab.text, _IdNumberLab.font, KScreenWidth, 20);
    _IdNumberLab.frame = CGRectMake(15, -25, IdNumberLabSize.width, IdNumberLabSize.height);

    CGSize _nameLabSize = textSizeFont(_nameLab.text, _nameLab.font, KScreenWidth, 20);
    _nameLab.frame = CGRectMake(15, _IdNumberLab.top-35, _nameLabSize.width, _nameLabSize.height);

    ageBtn.frame = CGRectMake(_nameLab.right+10, _nameLab.centerY-10, 50, 20);
    
    _constellationLab.frame = CGRectMake(ageBtn.right+5, _nameLab.centerY-10, 50, 20);

    [headeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15);
        make.height.mas_equalTo(75);
        make.top.equalTo(self).offset(-30);
        make.width.mas_equalTo(75);
    }];
    headeImage.layer.cornerRadius = headeImage.height/2;

    [headeImage sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(infoModel.photo)] placeholderImage:[UIImage imageNamed:@"llustration"]];
    SignatureLab.frame = CGRectMake(SignaLab.right+5, 12.5, self.width-headeImage.width-SignaLab.width-20, 0);
    SignatureLab.numberOfLines=0;

    
    
    if (infoModel.motto.length>0) {
        SignatureLab.text = infoModel.motto;
        [SignatureLab sizeToFit];

        SignatureLab.frame = CGRectMake(SignaLab.right+5, 12.5, self.width-headeImage.width-SignaLab.width-30, SignatureLab.height);

    }else{
        
        SignatureLab.text = @"您还没有填写签名哦";
        [SignatureLab sizeToFit];

        SignatureLab.frame = CGRectMake(SignaLab.right+10, 12.5, self.width-headeImage.width-SignaLab.width-30, SignatureLab.height);
    }
    
  
}
- (UIColor *)getColorWithStr:(NSString *)starStr{
    
    NSArray * starArr = @[@"白羊座",@"天秤座",@"金牛座",@"天蝎座",@"双子座",@"射手座",@"巨蟹座",@"摩羯座",@"狮子座",@"处女座",@"双鱼座"];
    NSArray * colorArr = @[ColorI(0xd80808),ColorI(0xec3db0),ColorI(0x2898df),ColorI(0xa651c9),ColorI(0xff6839),ColorI(0x77ca69),ColorI(0xd16739),ColorI(0x30d3d5),ColorI(0x19d7ad),ColorI(0xecac3d),ColorI(0x32b576)];
    
    
    for (NSString * star in starArr) {
        if ([star isEqualToString:starStr]) {
            NSInteger index = [starArr indexOfObject:star];
            if (colorArr[index]) {
                return colorArr[index];
            }
        }
    }
    return ColorI(0xd80808);
}
@end
